package com.yay.rental.ui;

import java.util.Calendar;

import com.yay.rental.R;
import com.yay.rental.provider.RentalContract.Category;
import com.yay.rental.provider.RentalContract.RentalMap;
import com.yay.rental.util.NotifyingAsyncQueryHandler;
import com.yay.rental.util.NotifyingAsyncQueryHandler.AsyncQueryListener;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


/**
 * Get Started!
 * @author team@getstarted.com.ua
 * 
 * {@link ListActivity} that displays a set of {@link Category}, as requested
 * through {@link Intent#getData()}.
 */
public class SearchActivity extends ListActivity implements AsyncQueryListener {
    private CategoryAdapter mAdapter;
    private NotifyingAsyncQueryHandler mHandler;
    static final int CI_DIALOG_ID = 0;
    static final int CO_DIALOG_ID = 1;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        mAdapter = new CategoryAdapter(this);
        setListAdapter(mAdapter);

        final Intent intent = getIntent();
        final Uri categoryUri = intent.getData();
        
        // Start background query to load categories
        mHandler = new NotifyingAsyncQueryHandler(getContentResolver(), this);
        mHandler.startQuery(categoryUri, CategoryQuery.PROJECTION, null, null, Category.DEFAULT_SORT);
    }

    /** {@inheritDoc} */
    public void onQueryComplete(int token, Object cookie, Cursor cursor) {
        startManagingCursor(cursor);
        mAdapter.changeCursor(cursor);
    }

    // Creating dialog
    @Override
    protected Dialog onCreateDialog(int id) {
	    Calendar c = Calendar.getInstance();
	    int cyear = c.get(Calendar.YEAR);
	    int cmonth = c.get(Calendar.MONTH);
	    int cday = c.get(Calendar.DAY_OF_MONTH);
	    Dialog dlg = new DatePickerDialog(this,  mDateSetListener,  cyear, cmonth, cday);
    	switch (id) {
		    case CI_DIALOG_ID:
		    	dlg.setTitle(getResources().getString(R.string.search_ci_date));
		    	return dlg;
		    case CO_DIALOG_ID:
		    	dlg.setTitle(getResources().getString(R.string.search_co_date));
		    	return dlg;
	    }
	    return null;
    }
    private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
	    // onDateSet method
	    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
		    String date_selected = String.valueOf(monthOfYear+1)+" /"+String.valueOf(dayOfMonth)+" /"+String.valueOf(year);
		    Toast.makeText(SearchActivity.this, "Selected Date is ="+date_selected, Toast.LENGTH_SHORT).show();
	    }
    };
    
    /** Handle "search" title-bar action. */
    public void onSearchClick(View v) {
        //UIUtils.goSearch(this);
    }
    
    /** Handle "check in" title-bar action. */
    public void onCheckInClick(View v) {
    	showDialog(CI_DIALOG_ID);
    }
    
    /** Handle "check out" title-bar action. */
    public void onCheckOutClick(View v) {
    	showDialog(CO_DIALOG_ID);
    }

    /** Handle "map" title-bar action. */
    public void onMapClick(View v) {
    	Intent intent = new Intent(Intent.ACTION_VIEW, RentalMap.CONTENT_URI);
    	startActivity(intent);
    }
    
    /** {@inheritDoc} */
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // Launch viewer for specific track
        final Cursor cursor = (Cursor)mAdapter.getItem(position);
        Intent intent = null;
        String idp = cursor.getString(CategoryQuery.IDP);
        if(!"0".equals(idp)) {
        	String categoryId = cursor.getString(CategoryQuery._ID);
            intent = new Intent(Intent.ACTION_VIEW, Category.buildRentalUri(categoryId));
        	intent.putExtra(Intent.EXTRA_TITLE, cursor.getString(CategoryQuery.NAME));
        	startActivity(intent);
        }
    }

    /**
     * {@link CursorAdapter} that renders a {@link DishQuery}.
     */
    private class CategoryAdapter extends CursorAdapter {
        public CategoryAdapter(Context context) {
            super(context, null);
        }

        /** {@inheritDoc} */
        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            return getLayoutInflater().inflate(R.layout.list_item_category, parent, false);
        }

        /** {@inheritDoc} */
        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            final TextView name = (TextView) view.findViewById(android.R.id.text1);
            name.setText(cursor.getString(CategoryQuery.NAME));
            String idp = cursor.getString(CategoryQuery.IDP);
            if(!"0".equals(idp)) {
            	name.setTextColor(SearchActivity.this.getResources().getColor(R.color.black));
            	name.setBackgroundColor(SearchActivity.this.getResources().getColor(R.color.white));
            } else {
            	name.setTextColor(SearchActivity.this.getResources().getColor(R.color.white));
            	name.setBackgroundColor(SearchActivity.this.getResources().getColor(R.color.title_background));
            }
        }
    }

    /** {@link Dish} query parameters. */
    private interface CategoryQuery {
        String[] PROJECTION = {
                Category._ID,
                Category.IDP,
                Category.NAME
        };

        int _ID = 0;
        int IDP = 1;
        int NAME = 2;
    }
    
}
